﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Approve.BLL
{
    /// <summary>
    /// 异常处理类
    /// </summary>
    public class ExceptionHelper : Exception
    {
        /// <summary>
        /// 异常类型Normal给客户，Debug不给客户
        /// </summary>
        public enum ExceptionType
        {
            Nothing,
            Throw,
            Debug
        }

        #region fields + constructor
        /// <summary>
        /// 是否有异常
        /// </summary>
        public bool Has { get; set; }

        /// <summary>
        /// 异常代码
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 异常消息
        /// </summary>
        public string Message { get; set; }


        public ExceptionHelper()
        { }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="exceptionType"></param>
        /// <param name="msg"></param>
        public ExceptionHelper(ExceptionType exceptionType, string msg)
        {
            this.Has = exceptionType == ExceptionType.Throw ? true : false;
            this.Has = exceptionType == ExceptionType.Debug ? true : this.Has;
            this.Code = exceptionType == ExceptionType.Throw ? "xxx0002" : string.Empty;
            this.Code = exceptionType == ExceptionType.Debug ? "xxx0003" : this.Code;
            this.Message = msg;
        }

        /// <summary>
        /// 没有错误
        /// </summary>
        /// <returns></returns>
        public static ExceptionHelper Nothing()
        {
            return new ExceptionHelper(ExceptionHelper.ExceptionType.Nothing, string.Empty);
        }

        /// <summary>
        /// 抛出错误
        /// </summary>
        /// <returns></returns>
        public static ExceptionHelper Throw(string errMsg)
        {
            return new ExceptionHelper(ExceptionHelper.ExceptionType.Throw, errMsg);
        }

        /// <summary>
        /// 抛出错误
        /// </summary>
        /// <returns></returns>
        public static ExceptionHelper Debug(string errMsg)
        {
            return new ExceptionHelper(ExceptionHelper.ExceptionType.Debug, errMsg);
        }

        /// <summary>
        /// 需要登陆的错误信息
        /// </summary>
        /// <returns></returns>
        public static ExceptionHelper NoSignIn(string message = "")
        {
            ExceptionHelper excpe = new ExceptionHelper();
            excpe.Has = true;
            excpe.Code = "xxx0001";
            excpe.Message = message.Trim() != "" ? message : "请您登录之后再操作！";
            return excpe;
        }
        #endregion
    }
}
